<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>redis键空间事件 | 欢迎来到小张的博客</title><meta name="keywords" content="数据库"><meta name="author" content="Z_SiTong"><meta name="copyright" content="Z_SiTong"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="什么是Redis上篇博客 “redis发布者订阅者” 已经介绍过啦，有兴趣的小伙伴可以翻翻看看哦 什么是键空间 Redis是一个键值对(key-value pair)数据库服务器，服务器中的每个数据库都由一个redis.h&#x2F;redisDb结构表示，redisDb结构都dict字典保存了数据库中的所有键值对，我们将这个字典称为键空间(key space)  什么是键空间通知 键空间通知使得客户端可以">
<meta property="og:type" content="article">
<meta property="og:title" content="redis键空间事件">
<meta property="og:url" content="https://zhang-sitong.gitee.io/blogs/lz_9/index.html">
<meta property="og:site_name" content="欢迎来到小张的博客">
<meta property="og:description" content="什么是Redis上篇博客 “redis发布者订阅者” 已经介绍过啦，有兴趣的小伙伴可以翻翻看看哦 什么是键空间 Redis是一个键值对(key-value pair)数据库服务器，服务器中的每个数据库都由一个redis.h&#x2F;redisDb结构表示，redisDb结构都dict字典保存了数据库中的所有键值对，我们将这个字典称为键空间(key space)  什么是键空间通知 键空间通知使得客户端可以">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/02/02/bGkq4nN38tABerD.jpg">
<meta property="article:published_time" content="2019-02-02T13:18:49.000Z">
<meta property="article:modified_time" content="2021-03-23T01:16:12.623Z">
<meta property="article:author" content="Z_SiTong">
<meta property="article:tag" content="数据库">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/02/02/bGkq4nN38tABerD.jpg"><link rel="shortcut icon" href="/blogs/img/favicon.png"><link rel="canonical" href="https://zhang-sitong.gitee.io/blogs/lz_9/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blogs/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blogs/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
};

const saveToLocal = {
  // ttl 單位是 天
  set: function setWithExpiry(key, value, ttl) {
    if (ttl === 0) return
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}

// https://stackoverflow.com/questions/16839698/jquery-getscript-alternative-in-native-javascript
const getScript = url => new Promise((resolve, reject) => {
  const script = document.createElement('script')
  script.src = url
  script.async = true
  script.onerror = reject
  script.onload = script.onreadystatechange = function() {
    const loadState = this.readyState
    if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
    script.onload = script.onreadystatechange = null
    resolve()
  }
  document.head.appendChild(script)
})</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-03-23 09:16:12'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = 'false'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><div class="aplayer" data-id="6583040939" data-server="netease" data-type="playlist" data-fixed="true" data-listFolded="false" data-order="random" data-preload="auto"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lete114/CDN/music/APlayer/APlayer.min.css"><script src="https://cdn.jsdelivr.net/gh/lete114/CDN/music/APlayer/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/meting@1.2.0/dist/Meting.min.js"></script><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/blogs/atom.xml" title="欢迎来到小张的博客" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/blogs/img/%E5%8D%9A%E5%AE%A2%E5%A4%B4%E5%83%8F.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blogs/archives/"><div class="headline">文章</div><div class="length-num">29</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blogs/tags/"><div class="headline">标签</div><div class="length-num">12</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blogs/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blogs/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/link/"><i class="fa-fw fa fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://i.loli.net/2021/02/02/bGkq4nN38tABerD.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blogs/">欢迎来到小张的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blogs/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/link/"><i class="fa-fw fa fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/blogs/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">redis键空间事件</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2019-02-02T13:18:49.000Z" title="undefined 2019-02-02 21:18:49">2019-02-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blogs/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="什么是Redis"><a href="#什么是Redis" class="headerlink" title="什么是Redis"></a>什么是Redis</h2><p><code>上篇博客 “redis发布者订阅者” 已经介绍过啦，有兴趣的小伙伴可以翻翻看看哦</code></p>
<h2 id="什么是键空间"><a href="#什么是键空间" class="headerlink" title="什么是键空间"></a>什么是键空间</h2><blockquote>
<p>Redis是一个键值对(key-value pair)数据库服务器，服务器中的每个数据库都由一个redis.h/redisDb结构表示，redisDb结构都dict字典保存了数据库中的所有键值对，我们将这个字典称为键空间(key space)</p>
</blockquote>
<h2 id="什么是键空间通知"><a href="#什么是键空间通知" class="headerlink" title="什么是键空间通知"></a>什么是键空间通知</h2><blockquote>
<p>键空间通知使得客户端可以通过订阅频道或模式， 来接收那些以某种方式改动了 Redis 数据集的事件</p>
</blockquote>
<h2 id="键空间原理"><a href="#键空间原理" class="headerlink" title="键空间原理"></a>键空间原理</h2><h4 id="键空间和用户所见对数据库是直接对应的："><a href="#键空间和用户所见对数据库是直接对应的：" class="headerlink" title="键空间和用户所见对数据库是直接对应的："></a>键空间和用户所见对数据库是直接对应的：</h4><blockquote>
<p>键空间的键也就是数据库的键，每个键都是一个字符串对象<br>键空间的值也就是数据库的值，每个值可以是字符串对象，列表对象，哈希表对象，集合对象和有序集合对象在内的任意一种Redis对象 </p>
</blockquote>
<hr>
<h2 id="启用键空间通知"><a href="#启用键空间通知" class="headerlink" title="启用键空间通知"></a>启用键空间通知</h2><ul>
<li><h4 id="默认情况下，禁用键空间事件通知，可以在redis-conf或redis-cli中启用"><a href="#默认情况下，禁用键空间事件通知，可以在redis-conf或redis-cli中启用" class="headerlink" title="默认情况下，禁用键空间事件通知，可以在redis.conf或redis-cli中启用"></a>默认情况下，禁用键空间事件通知，可以在redis.conf或redis-cli中启用</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">C:\Users\Lenovo&gt; redis-cli config set notify-keyspace-events KEA</span><br><span class="line">OK</span><br></pre></td></tr></table></figure>
<p><code>该KEA字符串意味着每一个可能的事件被启用</code></p>
</li>
<li><h4 id="检查事件是否正常"><a href="#检查事件是否正常" class="headerlink" title="检查事件是否正常"></a>检查事件是否正常</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">C:\Users\Lenovo&gt;redis-cli --csv psubscribe <span class="string">&#x27;*&#x27;</span></span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line"><span class="string">&quot;psubscribe&quot;</span>,<span class="string">&quot;&#x27;*&#x27;&quot;</span>,<span class="number">1</span></span><br></pre></td></tr></table></figure>
<p><code>psubscribe &#39;*&#39;意味着我们想要使用模式订阅所有事件*</code></p>
</li>
<li><h4 id="打开终端2输入-set-key1-value1"><a href="#打开终端2输入-set-key1-value1" class="headerlink" title="打开终端2输入 set key1 value1"></a>打开终端2输入 set key1 value1</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">C:\Users\Lenovo&gt;redis-cli</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; set key1 value1</span><br><span class="line">OK</span><br></pre></td></tr></table></figure></li>
<li><h4 id="再打开终端1会看到："><a href="#再打开终端1会看到：" class="headerlink" title="再打开终端1会看到："></a>再打开终端1会看到：</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">C:\Users\Lenovo&gt;redis-cli --csv psubscribe <span class="string">&#x27;*&#x27;</span>     </span><br><span class="line">Reading messages... (press Ctrl-C to quit)  </span><br><span class="line"><span class="string">&quot;psubscribe&quot;</span>,<span class="string">&quot;*&quot;</span>,<span class="number">1</span></span><br><span class="line"><span class="string">&quot;pmessage&quot;</span>,<span class="string">&quot;*&quot;</span>,<span class="string">&quot;__keyspace@0__:key1&quot;</span>,<span class="string">&quot;set&quot;</span></span><br><span class="line"><span class="string">&quot;pmessage&quot;</span>,<span class="string">&quot;*&quot;</span>,<span class="string">&quot;__keyevent@0__:set&quot;</span>,<span class="string">&quot;key1</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="事件的类型"><a href="#事件的类型" class="headerlink" title="事件的类型"></a>事件的类型</h2><p>对于每个修改数据库的操作，键空间通知都会发送<strong>两种不同类型的事件</strong><br>比如说，对0号数据库的键 mykey 执行 DEL key [key …] 命令时，系统将分发两条消息，相当于执行以下两个 PUBLISH channel message 命令：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">PUBLISH __keyspace@0__:mykey del</span><br><span class="line">PUBLISH __keyevent@0__:mykey zst</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>keyspace@0</strong>:mykey 可以接收 0 号数据库中所有修改键 mykey 的事件<br><strong>keyevent@0</strong>:del 则可以接收 0 号数据库中所有执行 del 命令的键<br>以 keyspace 为前缀的频道被称为键空间通知<br>以 keyevent 为前缀的频道则被称为键事件通知</p>
</blockquote>
<h2 id="键空间配置"><a href="#键空间配置" class="headerlink" title="键空间配置"></a>键空间配置</h2><blockquote>
<p>因为开启键空间通知功能需要消耗一些CPU，所以在默认配置下，该功能处于关闭状态<br>可以通过修改 redis.conf 文件，或者直接使用 CONFIG SET 命令来开启或关闭键空间通知功能:</p>
</blockquote>
<ul>
<li>当 notify-keyspace-events 选项的参数为空字符串时，功能关闭</li>
<li>当参数不是空字符串时，功能开启</li>
</ul>
<h4 id="notify-keyspace-events-的参数可以是以下字符的任意组合，-它指定了服务器该发送哪些类型的通知："><a href="#notify-keyspace-events-的参数可以是以下字符的任意组合，-它指定了服务器该发送哪些类型的通知：" class="headerlink" title="notify-keyspace-events 的参数可以是以下字符的任意组合， 它指定了服务器该发送哪些类型的通知："></a>notify-keyspace-events 的参数可以是以下字符的任意组合， 它指定了服务器该发送哪些类型的通知：</h4><table>
    <tr>
        <th>字符</th>
        <th>通知</th>
    </tr>
    <tr>
        <td>K</td>
        <td>键空间通知，所有通知以 __keyspace@<db>__ 为前缀</td>
    </tr>
    <tr>
        <td>E</td>
        <td>键事件通知，所有通知以 __keyevent@<db>__ 为前缀</td>
    </tr>
    <tr>
        <td>g</td>
        <td>DEL 、 EXPIRE 、 RENAME 等类型无关的通用命令的通知</td>
    </tr>
    <tr>
        <td>$</td>
        <td>字符串命令的通知</td>
    </tr>
    <tr>
        <td>l</td>
        <td>列表命令的通知</td>
    </tr>
    <tr>
        <td>s</td>
        <td>集合命令的通知</td>
    </tr>
    <tr>
        <td>h</td>
        <td>哈希命令的通知</td>
    </tr>
    <tr>
        <td>z</td>
        <td>有序集合命令的通知</td>
    </tr>
    <tr>
        <td>x</td>
        <td>过期事件：每当有过期键被删除时发送</td>
    </tr>
    <tr>
        <td>e</td>
        <td>驱逐(evict)事件：每当有键因为 maxmemory 政策而被删除时发送</td>
    </tr>
    <tr>
        <td>A</td>
        <td>参数 g$lshzxe 的别名，即all</td>
    </tr>
</table>

<hr>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><ul>
<li><h2 id="One"><a href="#One" class="headerlink" title="One"></a>One</h2><ul>
<li><h4 id="redis发布所有通知，客户端1001订阅所有消息"><a href="#redis发布所有通知，客户端1001订阅所有消息" class="headerlink" title="redis发布所有通知，客户端1001订阅所有消息"></a>redis发布所有通知，客户端1001订阅所有消息</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; config set notify-keyspace-events KEA</span><br><span class="line">OK</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; psubscribe __key*@0__:*</span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;psubscribe&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) (integer) <span class="number">1</span></span><br></pre></td></tr></table></figure></li>
<li><h4 id="在客户端1002上执行set和del命令"><a href="#在客户端1002上执行set和del命令" class="headerlink" title="在客户端1002上执行set和del命令"></a>在客户端1002上执行set和del命令</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; set msg <span class="string">&quot;hi sister&quot;</span></span><br><span class="line">OK</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; del msg</span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; del msg</span><br><span class="line">(integer) <span class="number">0</span></span><br></pre></td></tr></table></figure></li>
<li><h4 id="客户端1001的内容如下"><a href="#客户端1001的内容如下" class="headerlink" title="客户端1001的内容如下"></a>客户端1001的内容如下</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyspace@0__:msg&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;set&quot;</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyevent@0__:set&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;msg&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyspace@0__:msg&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;del&quot;</span></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyevent@0__:del&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;msg&quot;</span></span><br></pre></td></tr></table></figure>
<code>客户端1001收到了keyspace和keyevent消息</code></li>
</ul>
</li>
<li><h2 id="Two"><a href="#Two" class="headerlink" title="Two"></a>Two</h2><ul>
<li><h4 id="redis仅发布keyspace通知，而客户端1001订阅所有消息类型"><a href="#redis仅发布keyspace通知，而客户端1001订阅所有消息类型" class="headerlink" title="redis仅发布keyspace通知，而客户端1001订阅所有消息类型"></a>redis仅发布keyspace通知，而客户端1001订阅所有消息类型</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; config set notify-keyspace-events KA</span><br><span class="line">OK</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; psubscribe __key*@0__:*</span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;psubscribe&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) (integer) <span class="number">1</span></span><br></pre></td></tr></table></figure></li>
<li><h4 id="在客户端1002也执行以上操作，再来看客户端1001的内容"><a href="#在客户端1002也执行以上操作，再来看客户端1001的内容" class="headerlink" title="在客户端1002也执行以上操作，再来看客户端1001的内容"></a>在客户端1002也执行以上操作，再来看客户端1001的内容</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyspace@0__:msg&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;set&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyspace@0__:msg&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;del&quot;</span></span><br></pre></td></tr></table></figure>
<code>客户端1001订阅了所有消息，但是Redis仅发布了keyspace事件 在客户端1002上执行了两次del操作，只有第一个del成功执行了，从而产生了一个事件</code></li>
</ul>
</li>
<li><h2 id="Three"><a href="#Three" class="headerlink" title="Three"></a>Three</h2><ul>
<li><h4 id="redis发布所有通知，客户端1001仅订阅keyspace消息"><a href="#redis发布所有通知，客户端1001仅订阅keyspace消息" class="headerlink" title="redis发布所有通知，客户端1001仅订阅keyspace消息"></a>redis发布所有通知，客户端1001仅订阅keyspace消息</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; config set notify-keyspace-events KEA</span><br><span class="line">OK</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; psubscribe __keyspace@0__:*</span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;psubscribe&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyspace@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) (integer) <span class="number">1</span></span><br></pre></td></tr></table></figure></li>
<li><h4 id="在客户端1002也执行以上操作，再来看客户端1001的内容-1"><a href="#在客户端1002也执行以上操作，再来看客户端1001的内容-1" class="headerlink" title="在客户端1002也执行以上操作，再来看客户端1001的内容"></a>在客户端1002也执行以上操作，再来看客户端1001的内容</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyspace@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyspace@0__:msg&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;set&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__keyspace@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyspace@0__:msg&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;del&quot;</span></span><br></pre></td></tr></table></figure>
<code>客户端1001收到了keyspace消息</code></li>
</ul>
</li>
<li><h2 id="Four"><a href="#Four" class="headerlink" title="Four"></a>Four</h2><ul>
<li><h4 id="redis仅发布字符串特定类型的通知，客户端1001订阅所有类型的消息"><a href="#redis仅发布字符串特定类型的通知，客户端1001订阅所有类型的消息" class="headerlink" title="redis仅发布字符串特定类型的通知，客户端1001订阅所有类型的消息"></a>redis仅发布字符串特定类型的通知，客户端1001订阅所有类型的消息</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; config set notify-keyspace-events KE$</span><br><span class="line">OK</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt;  psubscribe __key*@0__:*</span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;psubscribe&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) (integer) <span class="number">1</span></span><br></pre></td></tr></table></figure></li>
<li><h4 id="在客户端1002上执行lpush、set和del命令"><a href="#在客户端1002上执行lpush、set和del命令" class="headerlink" title="在客户端1002上执行lpush、set和del命令"></a>在客户端1002上执行lpush、set和del命令</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; lpush alist <span class="number">1</span> <span class="number">2</span> <span class="number">3</span></span><br><span class="line">(integer) <span class="number">3</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; set msg <span class="string">&quot;hello brother&quot;</span></span><br><span class="line">OK</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; del msg</span><br><span class="line">(integer) <span class="number">1</span></span><br></pre></td></tr></table></figure></li>
<li><h4 id="客户端1001的内容如下-1"><a href="#客户端1001的内容如下-1" class="headerlink" title="客户端1001的内容如下"></a>客户端1001的内容如下</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyspace@0__:msg&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;set&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span>) <span class="string">&quot;pmessage&quot;</span></span><br><span class="line"><span class="number">2</span>) <span class="string">&quot;__key*@0__:*&quot;</span></span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;__keyevent@0__:set&quot;</span></span><br><span class="line"><span class="number">4</span>) <span class="string">&quot;msg&quot;</span></span><br></pre></td></tr></table></figure>
<code>针对字符串，产生了keyspace和keyevent类型的消息 针对列表的lpush命令并没有产生消息 del命令是非特定类型的命令，也没有产生消息</code></li>
</ul>
</li>
</ul>
<hr>
<blockquote>
<p>PS：今天的博客就到这里啦，如有疑问请留言哦！</p>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Z_SiTong</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://zhang-sitong.gitee.io/blogs/lz_9/">https://zhang-sitong.gitee.io/blogs/lz_9/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://zhang-sitong.gitee.io/blogs" target="_blank">欢迎来到小张的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blogs/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/02/02/bGkq4nN38tABerD.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blogs/lz_10/"><img class="prev-cover" src="https://i.loli.net/2021/02/03/eAlR4OJSjo6GWTD.jpg" onerror="onerror=null;src='/blogs/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">webshell</div></div></a></div><div class="next-post pull-right"><a href="/blogs/lz_8/"><img class="next-cover" src="https://i.loli.net/2021/02/01/H5oBbvFCQUTEsyW.jpg" onerror="onerror=null;src='/blogs/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">redis发布者订阅者</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blogs/lz_17/" title="MongoDB安装及使用"><img class="cover" src="https://i.loli.net/2021/03/17/7x6uAiswPjbJknS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-18</div><div class="title">MongoDB安装及使用</div></div></a></div><div><a href="/blogs/lz_18/" title="MySQL和NoSQL"><img class="cover" src="https://i.loli.net/2021/03/21/BNz2vCwnVTsXHrf.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-21</div><div class="title">MySQL和NoSQL</div></div></a></div><div><a href="/blogs/lz_24/" title="openLDAP"><img class="cover" src="https://i.loli.net/2021/04/07/nLeJybVMHwFNj7G.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-31</div><div class="title">openLDAP</div></div></a></div><div><a href="/blogs/lz_27/" title="基于Docker安装关系型数据库PostgrelSQL替代Mysql"><img class="cover" src="https://i.loli.net/2021/04/19/QRXbxEvnLpBHuzI.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-04-14</div><div class="title">基于Docker安装关系型数据库PostgrelSQL替代Mysql</div></div></a></div><div><a href="/blogs/lz_8/" title="redis发布者订阅者"><img class="cover" src="https://i.loli.net/2021/02/01/H5oBbvFCQUTEsyW.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-02-01</div><div class="title">redis发布者订阅者</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/blogs/img/%E5%8D%9A%E5%AE%A2%E5%A4%B4%E5%83%8F.jpg" onerror="this.onerror=null;this.src='/blogs/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Z_SiTong</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blogs/archives/"><div class="headline">文章</div><div class="length-num">29</div></a></div><div class="card-info-data-item is-center"><a href="/blogs/tags/"><div class="headline">标签</div><div class="length-num">12</div></a></div><div class="card-info-data-item is-center"><a href="/blogs/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFRedis"><span class="toc-number">1.</span> <span class="toc-text">什么是Redis</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E9%94%AE%E7%A9%BA%E9%97%B4"><span class="toc-number">2.</span> <span class="toc-text">什么是键空间</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E9%94%AE%E7%A9%BA%E9%97%B4%E9%80%9A%E7%9F%A5"><span class="toc-number">3.</span> <span class="toc-text">什么是键空间通知</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%94%AE%E7%A9%BA%E9%97%B4%E5%8E%9F%E7%90%86"><span class="toc-number">4.</span> <span class="toc-text">键空间原理</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%94%AE%E7%A9%BA%E9%97%B4%E5%92%8C%E7%94%A8%E6%88%B7%E6%89%80%E8%A7%81%E5%AF%B9%E6%95%B0%E6%8D%AE%E5%BA%93%E6%98%AF%E7%9B%B4%E6%8E%A5%E5%AF%B9%E5%BA%94%E7%9A%84%EF%BC%9A"><span class="toc-number">4.0.1.</span> <span class="toc-text">键空间和用户所见对数据库是直接对应的：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%AF%E7%94%A8%E9%94%AE%E7%A9%BA%E9%97%B4%E9%80%9A%E7%9F%A5"><span class="toc-number">5.</span> <span class="toc-text">启用键空间通知</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%BB%98%E8%AE%A4%E6%83%85%E5%86%B5%E4%B8%8B%EF%BC%8C%E7%A6%81%E7%94%A8%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6%E9%80%9A%E7%9F%A5%EF%BC%8C%E5%8F%AF%E4%BB%A5%E5%9C%A8redis-conf%E6%88%96redis-cli%E4%B8%AD%E5%90%AF%E7%94%A8"><span class="toc-number">5.0.1.</span> <span class="toc-text">默认情况下，禁用键空间事件通知，可以在redis.conf或redis-cli中启用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A3%80%E6%9F%A5%E4%BA%8B%E4%BB%B6%E6%98%AF%E5%90%A6%E6%AD%A3%E5%B8%B8"><span class="toc-number">5.0.2.</span> <span class="toc-text">检查事件是否正常</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%93%E5%BC%80%E7%BB%88%E7%AB%AF2%E8%BE%93%E5%85%A5-set-key1-value1"><span class="toc-number">5.0.3.</span> <span class="toc-text">打开终端2输入 set key1 value1</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%86%8D%E6%89%93%E5%BC%80%E7%BB%88%E7%AB%AF1%E4%BC%9A%E7%9C%8B%E5%88%B0%EF%BC%9A"><span class="toc-number">5.0.4.</span> <span class="toc-text">再打开终端1会看到：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E7%9A%84%E7%B1%BB%E5%9E%8B"><span class="toc-number">6.</span> <span class="toc-text">事件的类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%94%AE%E7%A9%BA%E9%97%B4%E9%85%8D%E7%BD%AE"><span class="toc-number">7.</span> <span class="toc-text">键空间配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#notify-keyspace-events-%E7%9A%84%E5%8F%82%E6%95%B0%E5%8F%AF%E4%BB%A5%E6%98%AF%E4%BB%A5%E4%B8%8B%E5%AD%97%E7%AC%A6%E7%9A%84%E4%BB%BB%E6%84%8F%E7%BB%84%E5%90%88%EF%BC%8C-%E5%AE%83%E6%8C%87%E5%AE%9A%E4%BA%86%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%AF%A5%E5%8F%91%E9%80%81%E5%93%AA%E4%BA%9B%E7%B1%BB%E5%9E%8B%E7%9A%84%E9%80%9A%E7%9F%A5%EF%BC%9A"><span class="toc-number">7.0.1.</span> <span class="toc-text">notify-keyspace-events 的参数可以是以下字符的任意组合， 它指定了服务器该发送哪些类型的通知：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0"><span class="toc-number">8.</span> <span class="toc-text">实现</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#One"><span class="toc-number">9.</span> <span class="toc-text">One</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#redis%E5%8F%91%E5%B8%83%E6%89%80%E6%9C%89%E9%80%9A%E7%9F%A5%EF%BC%8C%E5%AE%A2%E6%88%B7%E7%AB%AF1001%E8%AE%A2%E9%98%85%E6%89%80%E6%9C%89%E6%B6%88%E6%81%AF"><span class="toc-number">9.0.1.</span> <span class="toc-text">redis发布所有通知，客户端1001订阅所有消息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9C%A8%E5%AE%A2%E6%88%B7%E7%AB%AF1002%E4%B8%8A%E6%89%A7%E8%A1%8Cset%E5%92%8Cdel%E5%91%BD%E4%BB%A4"><span class="toc-number">9.0.2.</span> <span class="toc-text">在客户端1002上执行set和del命令</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF1001%E7%9A%84%E5%86%85%E5%AE%B9%E5%A6%82%E4%B8%8B"><span class="toc-number">9.0.3.</span> <span class="toc-text">客户端1001的内容如下</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Two"><span class="toc-number">10.</span> <span class="toc-text">Two</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#redis%E4%BB%85%E5%8F%91%E5%B8%83keyspace%E9%80%9A%E7%9F%A5%EF%BC%8C%E8%80%8C%E5%AE%A2%E6%88%B7%E7%AB%AF1001%E8%AE%A2%E9%98%85%E6%89%80%E6%9C%89%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B"><span class="toc-number">10.0.1.</span> <span class="toc-text">redis仅发布keyspace通知，而客户端1001订阅所有消息类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9C%A8%E5%AE%A2%E6%88%B7%E7%AB%AF1002%E4%B9%9F%E6%89%A7%E8%A1%8C%E4%BB%A5%E4%B8%8A%E6%93%8D%E4%BD%9C%EF%BC%8C%E5%86%8D%E6%9D%A5%E7%9C%8B%E5%AE%A2%E6%88%B7%E7%AB%AF1001%E7%9A%84%E5%86%85%E5%AE%B9"><span class="toc-number">10.0.2.</span> <span class="toc-text">在客户端1002也执行以上操作，再来看客户端1001的内容</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Three"><span class="toc-number">11.</span> <span class="toc-text">Three</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#redis%E5%8F%91%E5%B8%83%E6%89%80%E6%9C%89%E9%80%9A%E7%9F%A5%EF%BC%8C%E5%AE%A2%E6%88%B7%E7%AB%AF1001%E4%BB%85%E8%AE%A2%E9%98%85keyspace%E6%B6%88%E6%81%AF"><span class="toc-number">11.0.1.</span> <span class="toc-text">redis发布所有通知，客户端1001仅订阅keyspace消息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9C%A8%E5%AE%A2%E6%88%B7%E7%AB%AF1002%E4%B9%9F%E6%89%A7%E8%A1%8C%E4%BB%A5%E4%B8%8A%E6%93%8D%E4%BD%9C%EF%BC%8C%E5%86%8D%E6%9D%A5%E7%9C%8B%E5%AE%A2%E6%88%B7%E7%AB%AF1001%E7%9A%84%E5%86%85%E5%AE%B9-1"><span class="toc-number">11.0.2.</span> <span class="toc-text">在客户端1002也执行以上操作，再来看客户端1001的内容</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Four"><span class="toc-number">12.</span> <span class="toc-text">Four</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#redis%E4%BB%85%E5%8F%91%E5%B8%83%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%89%B9%E5%AE%9A%E7%B1%BB%E5%9E%8B%E7%9A%84%E9%80%9A%E7%9F%A5%EF%BC%8C%E5%AE%A2%E6%88%B7%E7%AB%AF1001%E8%AE%A2%E9%98%85%E6%89%80%E6%9C%89%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%B6%88%E6%81%AF"><span class="toc-number">12.0.1.</span> <span class="toc-text">redis仅发布字符串特定类型的通知，客户端1001订阅所有类型的消息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9C%A8%E5%AE%A2%E6%88%B7%E7%AB%AF1002%E4%B8%8A%E6%89%A7%E8%A1%8Clpush%E3%80%81set%E5%92%8Cdel%E5%91%BD%E4%BB%A4"><span class="toc-number">12.0.2.</span> <span class="toc-text">在客户端1002上执行lpush、set和del命令</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF1001%E7%9A%84%E5%86%85%E5%AE%B9%E5%A6%82%E4%B8%8B-1"><span class="toc-number">12.0.3.</span> <span class="toc-text">客户端1001的内容如下</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_28/" title="多因子登录基础知识点"><img src="https://i.loli.net/2021/04/19/T9cDNdmovulPEbL.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="多因子登录基础知识点"/></a><div class="content"><a class="title" href="/blogs/lz_28/" title="多因子登录基础知识点">多因子登录基础知识点</a><time datetime="2019-04-19T12:08:59.000Z" title="发表于 2019-04-19 20:08:59">2019-04-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_27/" title="基于Docker安装关系型数据库PostgrelSQL替代Mysql"><img src="https://i.loli.net/2021/04/19/QRXbxEvnLpBHuzI.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="基于Docker安装关系型数据库PostgrelSQL替代Mysql"/></a><div class="content"><a class="title" href="/blogs/lz_27/" title="基于Docker安装关系型数据库PostgrelSQL替代Mysql">基于Docker安装关系型数据库PostgrelSQL替代Mysql</a><time datetime="2019-04-14T01:42:58.000Z" title="发表于 2019-04-14 09:42:58">2019-04-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_26/" title="json与pickle"><img src="https://i.loli.net/2021/04/13/DtnL4OVQM7bZjId.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="json与pickle"/></a><div class="content"><a class="title" href="/blogs/lz_26/" title="json与pickle">json与pickle</a><time datetime="2019-04-12T07:48:41.000Z" title="发表于 2019-04-12 15:48:41">2019-04-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_25/" title="集合(set)的用法和使用场景，以及底层去重算法"><img src="https://i.loli.net/2021/04/08/Ji5dTUKsnoGjRBz.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="集合(set)的用法和使用场景，以及底层去重算法"/></a><div class="content"><a class="title" href="/blogs/lz_25/" title="集合(set)的用法和使用场景，以及底层去重算法">集合(set)的用法和使用场景，以及底层去重算法</a><time datetime="2019-04-06T02:48:41.000Z" title="发表于 2019-04-06 10:48:41">2019-04-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blogs/lz_24/" title="openLDAP"><img src="https://i.loli.net/2021/04/07/nLeJybVMHwFNj7G.jpg" onerror="this.onerror=null;this.src='/blogs/img/404.jpg'" alt="openLDAP"/></a><div class="content"><a class="title" href="/blogs/lz_24/" title="openLDAP">openLDAP</a><time datetime="2019-03-31T08:52:13.000Z" title="发表于 2019-03-31 16:52:13">2019-03-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 <i style="color:#FF6A6A;animation: announ_animation 0.8s linear infinite;" class="fa fa-heartbeat"></i> Z_SiTong</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/blogs/js/utils.js"></script><script src="/blogs/js/main.js"></script><script src="/blogs/js/search/local-search.js"></script><div class="js-pjax"></div><script src="https://cdn.jsdelivr.net/gh/lete114/CDN/Sum/sakura.js"></script><script src="https://cdn.jsdelivr.net/gh/lete114/CDN/Sum/title.js"></script>null<script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>